/**
 * 
 */
package jmath.function.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.*;
import jmath.function.*;
import java.util.*;
import jmath.function.meta.*;

/**
 * @author timon
 * 
 */
public class MinSearchEngine extends IOFunction implements Startable {
	private Log log = LogFactory.getLog(MinSearchEngine.class);

	private int searchTimesCount = 4;

	private int parallelSearchCount = 6;
	
	private String graphName="calc";

	public void setGraphName(String graphName) {
		this.graphName = graphName;
	}

	public void setSearchTimesCount(int searchTimesCount) {
		this.searchTimesCount = searchTimesCount;
	}

	public void setParallelSearchCount(int parallelSearchCount) {
		this.parallelSearchCount = parallelSearchCount;
	}

	private VolumedModel model;	
	
	public void start() {
		
		Assert.notNull(model);
		try {			
			for (int j = 0; j < searchTimesCount; j++) {				
				//ThreadedFunction threadedFunction = new ThreadedFunction();
				for (int i = 0; i < parallelSearchCount; i++) {					
					//threadedFunction.addToStartableList(model.getStartable());
					model.getStartable().start();
				}				
				//threadedFunction.start();
				model.halfVolume();
				model.showResult(jMathConsole, jMathGraphPloter,graphName);
				if(hasDefined(model.getVolume().getParamIntervalMap())){
					break;
				}
			}
		} catch (Throwable ex) {
			ex.printStackTrace();
			log.debug(ex);
		}

	}
	
	private boolean hasDefined(Map<String,Interval> intervalMap){
		for(String param:intervalMap.keySet()){
			Interval interval=intervalMap.get(param);
			if(!interval.getLower().equals(interval.getUpper())){
				return false;
			}
		}
		return true;
	}

	

	public void setModel(VolumedModel model) {
		this.model = model;		
	}
	

}
